-- ShopDrawRender

local ClassRef = app.mvc.createListViewCell(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ShopDrawRender))
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )  
    self:refresh()

    self._ui["Panel/bg"]:setTouchEnabled(true)
    self._ui["Panel/bg"]:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended and self._cellModel and self._cellModel:getIndex() ~= self._cellModel:getFoucsIndex() then
            self:dispatchCellEvent("click", self._cellModel)
        end
    end)
end

function ClassRef:refresh()
    if self._data then
        self:refreshBrightStatus()
        local data = self._data
        local conf = data.config
        self._info = data.info
        self._ui.name:setString(L(conf.titleName))
        local iconPath = string.format("%s%s.png",Res.activityIconPath,conf.UI)
        self._ui.icon:loadTexture(iconPath)

        self:refreshTime()
    end
end

function ClassRef:refreshTime()
    if self._delayAct then
		self:stopAction(self._delayAct)
		self._delayAct = nil
	end
    local endTime = self._info.endTime
    local serverTime = app.session:getServerTime() or os.time()
    local param = TimeUtil.secondsToStr((endTime - serverTime), 4)
    if not param then
        self._ui.tips:setVisible(false)
        self._ui.Panel_time:setVisible(false)
    else
        self._ui.tips:setVisible(true)
        self._ui.Panel_time:setVisible(true)
        if param[1] then
            self._ui["Panel/Panel_time/time"]:setString(string.format(L("countdowen_day"), param[1]))
            self._ui["Panel/Panel_time/time"]:setColor(cc.c3b(158,236,0))
        elseif param[2] then
            self._ui["Panel/Panel_time/time"]:setString(string.format(L("countdowen_hour"), param[2]))
            if param[2] < 3 then
                self._ui["Panel/Panel_time/time"]:setColor(display.COLOR_RED)
            end
        elseif param[3] then
            self._ui["Panel/Panel_time/time"]:setString(string.format(L("countdowen_min"), param[3]))
            self._ui["Panel/Panel_time/time"]:setColor(display.COLOR_RED)
        end
        local delayTime = 1 -- 延时多长时间刷新
        if param[1] then
            if param[2] then
                delayTime = param[2]*3600
            elseif param[3] then
                delayTime = param[3]*60
            end
        elseif param[2] then
            delayTime = param[3]*60
        elseif param[3] then
            delayTime = 60
        end
        self._delayAct = self:performWithDelay(function()
            self._delayAct = nil
            self:refreshTime()
        end, delayTime)
    end
end

function ClassRef:refreshBrightStatus()
    if self._cellModel then
        self._ui["Panel/choose"]:setVisible(self._cellModel:getIndex() == self._cellModel:getFoucsIndex())
        self._ui["Panel/mask"]:setVisible(self._cellModel:getIndex() ~= self._cellModel:getFoucsIndex())
    else
        self._ui["Panel/choose"]:setVisible(false)
        self._ui["Panel/mask"]:setVisible(true)
    end
end

function ClassRef:onFoucsIndexChange(foucsIndex)
    self:refreshBrightStatus()
end

return ClassRef